package com.tomorrow.weather.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tomorrow.analyzeFactory.XMLParseFactory;
import com.tomorrow.weather.dbmanager.DBManager;
import com.tomorrow.weather.entity.City;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AddCityActivity extends Activity
{
	private GridView gvDefaultCity;
	private ArrayList<City> hotCityList;
	private Button btAddCityBackBt;
	private boolean firstLogin;
	private EditText etAddCityEdit;
	private TextView tvTextDefaultCity;
	private DBManager dbManager;
	private ArrayList<City> addCitiesList;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.bg_na);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.add_city);

		btAddCityBackBt = (Button) findViewById(R.id.add_city_back);
		etAddCityEdit = (EditText) findViewById(R.id.add_city_edit);
		tvTextDefaultCity = (TextView) findViewById(R.id.TextDefaultCity);
		dbManager = new DBManager(this);
		Bundle extras = getIntent().getExtras();

		if (extras == null)
		{
			btAddCityBackBt.setVisibility(View.GONE);
		}
		gvDefaultCity = (GridView) findViewById(R.id.GridDefaultCity);

		hotCityList = (ArrayList<City>) XMLParseFactory.getHotCities()
				.getHotCity();
		etAddCityEdit.addTextChangedListener(new AddCityTextWatcher());
		gvDefaultCity.setAdapter(new HotCityAdapter());
	}

	class AddCityTextWatcher implements TextWatcher
	{

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after)
		{

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count)
		{
			String city = etAddCityEdit.getText().toString().trim();
			if (!TextUtils.isEmpty(city))
			{
				tvTextDefaultCity.setVisibility(View.GONE);
				gvDefaultCity.setNumColumns(1);
				dbManager.openDatabase();
				addCitiesList = dbManager.getCities(city);
				dbManager.closeDatabase();
				gvDefaultCity.setAdapter(new HotCityAdapter(addCitiesList));
				gvDefaultCity.invalidate();
			}
			else {
				tvTextDefaultCity.setVisibility(View.VISIBLE);
				gvDefaultCity.setNumColumns(3);
				gvDefaultCity.setAdapter(new HotCityAdapter());
				gvDefaultCity.invalidate();
			}
			
		}

		@Override
		public void afterTextChanged(Editable s)
		{

		}

	}

	class AddHotCityAdapter extends CursorAdapter
	{

		public AddHotCityAdapter(Context context, Cursor c)
		{
			super(context, c);
		 
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent)
		{
			 
			return null;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor)
		{
			 
			
		}
		
	}
	class HotCityAdapter extends BaseAdapter
	{
		private List<City> cityList;
		public HotCityAdapter()
		{
			 this.cityList = hotCityList;
		}
		public HotCityAdapter(List<City> cityList)
		{
			this.cityList = cityList;
		}
		@Override
		public int getCount()
		{

			return cityList.size();
		}

		@Override
		public Object getItem(int position)
		{

			return cityList.get(position);
		}

		@Override
		public long getItemId(int position)
		{

			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			if (convertView == null)
			{
				LayoutInflater layoutInflater = getLayoutInflater();
				convertView = layoutInflater.inflate(R.layout.hotcity, null);
			}
			TextView cityNametv = (TextView) convertView
					.findViewById(R.id.hotcityItem);
			cityNametv.setText(cityList.get(position).getCityName());
			return convertView;
		}

	}
}
